using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace CosyTrade.UI
{
    public class Functions
    {
        public static void ShowMessageBox(string messageFormat, params string[] args)
        {
            ShowMessageBox(string.Format(messageFormat, args));
        }

        public static void ShowMessageBox(string message)
        {
            XtraMessageBox.Show(message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        public static void SendTabKey(Form form)
        {
            Control currentControl = form.ActiveControl;
            form.SelectNextControl(currentControl, true, true, true, true);
            form.ActiveControl = currentControl;
        }

        public static DialogResult ShowConfigBox(string messageFormat, params object[] param)
        {
            return ShowConfigBox(string.Format(messageFormat, param));
        }

        public static DialogResult ShowConfigBox(string message)
        {
            return XtraMessageBox.Show(message, Application.ProductName, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
        }

        public static bool ValueIsEmpty(Control control)
        {
            if (string.IsNullOrEmpty(control.Text))
            {
                control.Focus();
                return true;
            }
            else return false;
        }

        public static bool ValueIsEmpty(Control control, string showMessage)
        {
            bool reVal = ValueIsEmpty(control);
            if (reVal) ShowMessageBox(showMessage);
            return reVal;
        }

        public static string ToDBC(string input)
        {
            char[] c = input.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] == 12288)
                {
                    c[i] = (char)32;
                    continue;
                }
                if (c[i] > 65280 && c[i] < 65375)
                    c[i] = (char)(c[i] - 65248);
            }
            return new string(c);
        }

        public static byte[] GetMiniImage(string imgUrl)
        {
            using(Image sourceImage = Image.FromFile(imgUrl))
            {
                int baseWidth = 800;
                if (sourceImage.Width < baseWidth) baseWidth = sourceImage.Width;
                Image reVal = new Bitmap(baseWidth, baseWidth * sourceImage.Height / sourceImage.Width);
                using (Graphics g = Graphics.FromImage(reVal))
                    g.DrawImage(sourceImage, 0, 0, reVal.Width, reVal.Height);
                using (MemoryStream ms = new MemoryStream())
                {
                    sourceImage.Save(ms, ImageFormat.Jpeg);
                    return ms.GetBuffer();
                }
            }
        }

        public static string GetSearchString(string normailStr)
        {
            string reVal = normailStr.Replace('*', '%');
            if (reVal.IndexOf('%') == -1)
                reVal += '%';
            return reVal;
        }

        public static bool CheckNumber(object obj, ref int value)
        {
            if (obj == null) return false;
            string strVal = obj.ToString();
            return int.TryParse(strVal, out value);
        }

        public static bool CheckNumber(object obj, ref double value)
        {
            if (obj == null) return false;
            string strVal = obj.ToString();
            return double.TryParse(strVal, out value);
        }
    }
}